package newx.com.control.table
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	import newx.NxSprite;
	import newx.com.ResManager;
	import newx.com.control.button.NxButton;
	
	/**
	 * 在table中自定义创建所需的控件,应用例子详见：ChallengeUI.as
	 * @author wangpg
	 * 
	 */	
	public class TableCellFactory extends EventDispatcher
	{
		//父容器
		private var _container:Sprite
		//table
		private var _table:NxTable;
		//挑战按钮数组
		private var _arrTableCell:Array;
		//控件
		private var _control:*;
		//第二个控件
		private var _seccontrol:*;
		//控件在表格中一行的位置
		private var _colPos:int;
		//换页时数据自定义
		private var _tablePageChangeData:Function;
		//是否居中		
		private var _isCenter:Boolean = true;
		//第二个控件增加x值
		private var _x:int		= 0;
		
		public function TableCellFactory(table:NxTable,container:Sprite,
									 control:*,colPos:int,isCenter:Boolean = true,
									 seccontrol:* = null, x:int = 0)
		{
			_table = table;
			_container = container;
			_control = control;
			_seccontrol = seccontrol;
			_colPos = colPos;
			_isCenter = isCenter;
			_x	= x;
			addEvent();
		}
		
		private function addEvent():void
		{
//			_table.addEventListener(NxTableEvent.TABLE_COLUMN_SIZE_CHANGE, 
//				tableColumnSizeChange);
			_table.addEventListener(NxTableEvent.TABLE_PAGE_CHANGE, tableTurn);
		}
		private function   removeEvent():void
		{
//			_table.removeEventListener(NxTableEvent.TABLE_COLUMN_SIZE_CHANGE, 
//				tableColumnSizeChange);
			_table.removeEventListener(NxTableEvent.TABLE_PAGE_CHANGE, tableTurn);
		}
		
		public function tableTurn(e:NxTablePageEvent=null):void
		{
			//clearbtn
			if(_arrTableCell == null)
			{
				_arrTableCell = new Array();
			}
			var pageCount:int = _table.pageLinesConut;
			for(var i:int=0; i < pageCount; i++)
			{
				var pn:String	=	"";
				//取出当前表格数据
				if(e == null)
				{
					pn	=	_table.getItemFrom(
						new NxTabelPos(i));
				}
				else if(e.page >= 0)
				{
					pn	=	_table.getItemFrom(
						new NxTabelPos(i+e.page*pageCount));
				}
				
				var cell:DisplayObject = _arrTableCell[i];
				if(pn == "" || pn == null)
				{
					//如果无效，判断按钮是否存在，如果存在则删除					
					if(cell != null)
					{						
						_container.removeChild(cell);
						_arrTableCell[i] = null;
					}
				}
				else
				{
					//如果有,取按钮实例
					if(cell == null)		
					{
						//取位置	
						var px:Number = 0;
						var py:Number = 0;
						for(var j:int=0; j < _colPos; ++j)
						{
							px += _table.columWidthArr[j];
						}
						
						py = (i) * _table.itemHeight+_table.titleHeight;
						
						var cs:String = getQualifiedClassName(_control);	
						switch(cs)
						{
							//创建button
							case "newx.com.control.button::NxButton":
								cell = createButtonCell(_control, px,py);
								break;
							case "flash.text::TextField":
								cell=createTextField(px,py);
								break
							default :
								break;
						}
						
						_arrTableCell[i] = cell;
						_container.addChild(cell);
					}
				}
			}
			if(!_isCenter){
				for(i=0; i < pageCount; i++)
				{
					//取出当前表格数据
					if(e == null)
					{
						pn	=	_table.getItemFrom(
							new NxTabelPos(i));
					}
					else if(e.page >= 0)
					{
						pn	=	_table.getItemFrom(
							new NxTabelPos(i+e.page*pageCount));
					}
					
					cell = _arrTableCell[i + pageCount];
					if(pn == "" || pn == null)
					{
						//如果无效，判断按钮是否存在，如果存在则删除					
						if(cell != null)
						{						
							_container.removeChild(cell);
							_arrTableCell[i + pageCount] = null;
						}
					}
					else
					{
						//如果有,取按钮实例
						if(cell == null)		
						{
							//取位置	
							px = 0;
							py = 0;
							for(j=0; j < _colPos; ++j)
							{
								px += _table.columWidthArr[j] + _x;
							}
							
							py = (i) * _table.itemHeight+_table.titleHeight;
							
							cs = getQualifiedClassName(_seccontrol);	
							switch(cs)
							{
								//创建button
								case "newx.com.control.button::NxButton":
									cell = createButtonCell(_seccontrol, px, py);
									break;
								case "flash.text::TextField":
									cell=createTextField(px,py);
									break
								default :
									break;
							}
							
							_arrTableCell[i + pageCount] = cell;
							_container.addChild(cell);
						}
					}
				}
			}
			
			//数据处理
			if(_tablePageChangeData!=null)
				_tablePageChangeData.call();
//			_table.dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_COLUMN_SIZE_CHANGE,null));
		}
		
		//列宽改变时
		private function tableColumnSizeChange(e:NxTableEvent):void
		{
			var arrColWid:Array = _table.columWidthArr;
			
			if(arrColWid[arrColWid.length - 1] < _control.width + 8)
			{
				var colwid:int = 70 - arrColWid[arrColWid.length - 1]; 
				arrColWid[arrColWid.length - 1] = _control.width + 8;
				arrColWid[arrColWid.length - 2] -= colwid;
				_table.columWidthArr = arrColWid;
			}
			
			var posX:Number = 0; 
			for(var i:int = 0; i < _colPos; ++i)
			{
				posX += arrColWid[i];				
			}
			if(_isCenter){
				posX += (arrColWid[_colPos] - _control.width)/2 + _table.x;
			}
			
			for each(var con:* in _arrTableCell)
			{
				if(con==null)
					continue;
				con.x = posX;
			}
		}
		
		//获取行数组
		public function get ArrTabelCell():Array
		{
			return _arrTableCell;
		}
		
		//外部数据处理函数
		public function set TablePageChangeData(f:Function):void
		{
			_tablePageChangeData = f;
		}
		public function destroy():void
		{
			if(_arrTableCell)
			{
				for (var i:int = 0; i < _arrTableCell.length; i++) 
				{
					var cell:DisplayObject=_arrTableCell[i];
					if(cell)
					cell.parent.removeChild(cell);
				}
				_arrTableCell=null;
			}
			removeEvent()
			
		}
		//在table里创建button cell 
		private function createButtonCell(_btnControl:NxButton, px:Number, py:Number):NxButton
		{
			var btnCell:NxButton = new NxButton();
			//创建button	
			if(_isCenter){
				btnCell.x = px + _table.x + (
					_table.columWidthArr[_colPos] 
					- _btnControl.width)/2;
			} else {
				btnCell.x = px + _table.x + 20;
			}
			btnCell.y = py + _table.y + 2;	
			btnCell.skinWidth = _btnControl.width;
			btnCell.skinHeight = _table.itemHeight - 4;
			btnCell.skin = (_btnControl as NxButton).skin;
			btnCell.label = (_btnControl as NxButton).label;
			btnCell.visible = true;
			return btnCell;
		}
		//在table里创建textfield
		private function createTextField(px:Number,py:Number):TextField
		{
			var tCell:TextField = new TextField();
			var table:NxTable=_table;
			var itemNum:Number=_table.contentSkin.length;
			var p:Point=new Point(px,py);
			p=_container.globalToLocal(_table.localToGlobal(p))
			tCell.x = p.x + 20;
			tCell.y = p.y//py + _table.y + 2;
			tCell.width =table.columWidthArr[_colPos]// _control.width;
			tCell.height= table.itemHeight ;
			tCell.htmlText = (_control as TextField).htmlText;
			tCell.selectable=false;
			tCell.textColor	=	table.defaultTxtColor;
			tCell.defaultTextFormat.align=_table.itemAlign;
			return tCell;
		}
		
		//可以创建其他控件cell...
		
		
		
	}
}